package com.example.experiment3;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class QuizProvider extends ContentProvider {
    private static final int QUIZ = 0;
    private static final int COURSE = 1;

    private static final String AUTHORITY =
            "com.example.experiment3.provider";

    //1 想使用内容提供者 必须定义 匹配规则
    // code:定义的匹配规则 如果 匹配不上  有一个返回码  -1
    static UriMatcher matcher;

    private DBQuizHelper helper;
    private String table = "quiz";
    private static UriMatcher uriMatcher;

    static{
        //开始添加匹配规则
        /**
         * authority   主机名  通过主机名来访问共享的数据
         * path   pers.cnzdy.tutorial.quiz.provider
         * code 匹配码
         */
        matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(AUTHORITY, "quiz", QUIZ);
        matcher.addURI(AUTHORITY, "course", COURSE);
    }

    @Override
    public boolean onCreate() {
        helper = new DBQuizHelper(getContext(), "Exam.db", null, 1);
        return true;
    }

    @Override
    public String getType(Uri uri) {
//        switch (uriMatcher.match(uri)) {
//            case QUIZ:
//                return "vnd.android.cursor.dir/vnd.com.example.experiment3.provider.quiz";
//        }
        return "vnd.android.cursor.dir/quiz";

//        return null;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int match = matcher.match(uri);

        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
                Cursor cursor = null;
                cursor = db.query("quiz", projection, selection,
                        selectionArgs, null, null, sortOrder);

//            getContext().getContentResolver().notifyChange(uri, null);
//            Log.d("Provider", "Cursor");

            return cursor;
        } else {
            throw new IllegalArgumentException("路径匹配失败");
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int match = matcher.match(uri);

        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
            long rowId = db.insert(table, null, values);

            if (rowId > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            Uri uriResult = Uri.parse("content://"+ AUTHORITY + "/quiz/" + rowId);
            return uriResult;
        }else{
            throw new IllegalArgumentException("路径匹配失败");
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int match = matcher.match(uri);

        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
            int delete = db.delete(table, selection, selectionArgs);
            if (delete > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            return delete;
        }else {
            throw new IllegalArgumentException("路径匹配失败");
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int match = matcher.match(uri);
        if (match == QUIZ) {
            SQLiteDatabase db = helper.getReadableDatabase();
            int update = db.update(table, values, selection, selectionArgs);
            if (update > 0) {
                getContext().getContentResolver().notifyChange(uri, null);
            }
            return update;
        }else {
            throw new IllegalArgumentException("路径匹配失败");
        }
    }
}
